package com.kundi.dooradmin.controller;

import com.kundi.dooradmin.entity.Product;
import com.kundi.dooradmin.entity.Team;
import com.kundi.dooradmin.service.TeamService;
import com.kundi.dooradmin.util.MapUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

/**
 * Author 赵恒
 * Date 2020/7/10
 */
@Controller
@RequestMapping("/team")
public class TeamController {
    @Resource
    TeamService teamService;

    /**
     * 向数据库中添加团队信息
     * @param team
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping("/insert")
    @ResponseBody
    public Map<String, String> insert(Team team, @RequestBody MultipartFile file) throws IOException {
        team.setImage(file.getBytes());
        Integer i = teamService.insert(team);
        if(i>0) {
            return MapUtil.responseMap("添加成功", "true");
        } else {
            return MapUtil.responseMap("添加失败", "false");
        }
    }

    /**
     * 更换图片之后提交的修改
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping("/update")
    @ResponseBody
    public Map<String, String> update(Team team, @RequestBody MultipartFile file) throws  IOException {
        team.setImage(file.getBytes());
        Integer i = teamService.update(team);
        if(i>0) {
            return MapUtil.responseMap("修改成功", "true");
        } else {
            return MapUtil.responseMap("修改失败", "false");
        }
    }

    /**
     * 不更换图片的情况下，修改人物信息
     * @param team
     * @return
     */
    @RequestMapping("/updateNoImage")
    @ResponseBody
    public Map<String, String> updateNoImage(Team team) {
        Integer i = teamService.update(team);
        if(i>0) {
            return MapUtil.responseMap("修改成功", "true");
        } else {
            return MapUtil.responseMap("修改失败", "false");
        }
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Map<String, String> delete(Integer id) {
        Integer i = teamService.delete(id);
        if(i>0) {
            return MapUtil.responseMap("删除成功", "true");
        } else {
            return MapUtil.responseMap("删除失败", "false");
        }
    }



    /**
     * 获取所有的团队成员信息
     * @return
     */
    @RequestMapping("/getAll")
    @ResponseBody
    public List<Team> getAll() {
        return teamService.getAll();
    }

    /**
     * 根据ID获取图片
     * @param id
     * @param response
     * @throws IOException
     */
    @RequestMapping("/getImage")
    public void getImage(Integer id, HttpServletResponse response) throws IOException {
        Team team = teamService.getTeamById(id);
        response.setContentType("image/png");
        OutputStream output = response.getOutputStream();
        ByteArrayInputStream in = new ByteArrayInputStream(team.getImage());
        int len;
        byte[] buf = new byte[1024];
        while ((len = in.read(buf)) != -1) {
            output.write(buf, 0, len);
        }
        output.flush();
        output.close();
    }

    /**
     * 通过id获取成员信息
     * @param id
     * @return
     */
    @RequestMapping("/getTeamNoImage")
    @ResponseBody
    public Team getTeamNoImage(Integer id) {
        return teamService.getTeamNoImage(id);
    }
}
